<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Financial;
use App\Models\Inverstment;
use App\Models\Investment;
use App\Models\News;
use App\Models\Order;
use App\Models\Question;
use App\Models\QuestionType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\FinancialType;


class FinancialController extends Controller
{
    //显示内容
    public function anyType(Request $request)
    {
        //搜索功能
        $items = DB::table('financial_type');
        $items = $items->leftJoin('financial_type as financial_type_left', 'financial_type.parentID', '=', 'financial_type_left.id');
        if($request->has('keyword') && $request->input('keyword')){
            $items->where('name', 'like', '%'.$request->input('keyword').'%')
                ->orWhere('description', 'like', '%'.$request->input('keyword').'%');
        }
        $items = $items->select('financial_type.*', 'financial_type_left.name as parentName');

        $items = $items
            ->orderBy('updated_at','desc')
            ->paginate(20);
        return view('admin.financialtype.index',[
            'items' =>$items
        ]);
    }
    public function getEditftype(Request $request, $id=0)
    {
        //理财产品----产品类型管理的添加修改
        $ftype = FinancialType::findOrNew($id);
        //print_r($id);exit();
        $data = FinancialType::where('parentID',0)->get();
        return view('admin.financialtype.edit',[
            'id' => $id,
            'data' => $data,
            'ftype' => $ftype
        ]);
    }
    public function postEditftype(Request $request,$id=0)
    {
        //新增也就是发布消息
        $model = FinancialType::findOrNew($id);
        $data = $request->all();
        unset($data['_token']);
        $attributes = [
            'name'=>'类型名称',
            'description'=>'类型描述',
            'parentID' => '父ID',
        ];
        $validator = \Validator::make(
            $data,
            [
                'name' => 'required',
                'description' => 'required',
                 'parentID' => 'required',
            ],
            [
                'required' => ":attribute不能为空",
            ],
            $attributes
        );
        if($validator->fails()){
            //判断加入的数据不能为空
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        }else{
            $model->fill($data);
            $model->save();//写入数据库
            if($model && $model->id>0){
                $ret = ['state' => 'success', 'message' => '操作成功'];
            }else{
                $ret = ['state' => 'fail', 'message' => '操作失败'];
            }
        }
        return \Response::json($ret);
    }
    public function postDelete($id)
    {
        //产品类型管理的删除
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if (FinancialType::destroy($id)) {
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }
    public function anyFinancial(Request $request)
    {
        //产品管理---搜索功能
        $items = DB::table('financial');
        if($request->has('keyword') && $request->input('keyword')){
            $items->where('name','like','%'.$request->input('keyword').'%')
                ->orwhere('description','like','%'.$request->input('keyword').'%')
                ->orwhere('title','like','%'.$request->input('keyword').'%');
        }
        $items = $items
            ->leftJoin('financial_type','financial.fproductTypeId','=','financial_type.id')
            ->select('financial.*','financial_type.name');
        if($request->has('isonline') && (int)$request['isonline']>-1){
            $items = $items->where('isonline', '=', $request->input('isonline'));
        }
        $items = $items->orderBy('updated_at','desc')->paginate(20);
        return view('admin.financial.index',[
            'items' => $items,
            'appends' => ['keyword'=>$request->input('keyword'), 'isonline'=>$request->input('isonline')]
        ]);


    }
    public function getEditfina(Request $request,$id=0)
    {
        //产品管理的添加修改
        $item = Financial::findOrNew($id);
        $types = FinancialType::all();
        $item['images'] = explode(',', $item['images']);
        $item['pawnimg'] = explode(',', $item['pawnimg']);
        $item['messageimg'] = explode(',', $item['messageimg']);
        return view('admin.financial.edite',[
            'item' => $item,
            'types' => $types,
        ]);
    }
    public function postEditfina(Request $request,$id=0)
    {

        $model = Financial::findOrNew($id);
        $data = $request->all();
        unset($data['_token']);
        $attributes = [
            'fproductTypeId'=>'产品类别',
            'productNo'=>'产品代码',

        ];
        $validator = \Validator::make(
            $data,
            [
                'fproductTypeId' => 'required',
                'productNo' => 'required',
            ],
            [
                'required' => ":attribute不能为空",
            ],
            $attributes
        );
        if($validator->fails()){
            //判断加入的数据不能为空
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }

        }else{
            $data['images'] = ( $request->has('images') ) ? implode(',', $data['images']) :  $data['images'] = '';
//            if($request->has('images') ){
//                $data['images'] = implode(',', $data['images']) ;
//            }
            $data['pawnimg'] = ( $request->has('pawnimg') ) ? implode(',', $data['pawnimg']) :  $data['pawnimg'] = '';
            $data['messageimg'] = ( $request->has('messageimg') ) ? implode(',', $data['messageimg']) :  $data['messageimg'] = '';
            $model->fill($data);
            $model->save();//写入数据库
            if($model && $model->id>0){
                $ret = ['state' => 'success', 'message' => '操作成功'];
            }else{
                $ret = ['state' => 'fail', 'message' => '操作失败'];
            }
        }
        return \Response::json($ret);
    }
    public function getExplain($id)
    {
        $item = Financial::findOrNew($id);

        return view('admin.financial.explain',[
            'item' => $item,
        ]);
    }
    public function postExplain(Request $request,$id=0)
    {
        $model = Financial::findOrNew($id);
        $data = $request->all();
        unset($data['_token']);
        $attributes = [
            'gift'=>'新手有礼',
            'explain'=>'特别说明',
            'activity' => '活动详情',
        ];
        $validator = \Validator::make(
            $data,
            [
            'gift' => 'required',
            'explain' => 'required',
            'activity' => 'required',
            ],
            [
                'required' => ":attribute不能为空",
            ],
            $attributes
        );
        if($validator->fails()){
            //判断加入的数据不能为空
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }

        }else{
            $model->fill($data);
            $model->save();//写入数据库
            if($model && $model->id>0){
                $ret = ['state' => 'success', 'message' => '操作成功'];
            }else{
                $ret = ['state' => 'fail', 'message' => '操作失败'];
            }
        }
        return \Response::json($ret);
    }
    public function getPackage($id)
    {
        $item = Investment::where('financialId', $id)->get();

        return view('admin.financial.package',[
            'id'    => $id,
            'item' => $item,
        ]);
    }
    public function postDeletepackage($id) #删除礼包按钮
    {
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if (Investment::destroy($id)) {
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }
    public function postPackage(Request $request,$id=0)
    {
        $data = $request->all();
        $attributes = [
            'price'=>'可投资金额',
            'cycle'=>'收益周期',
            'mold' => '投资金额类型',
            'cycle' => '收益周期',
            'income' => '年化收益',
            'end' => '结束收益'
        ];
        $validator = \Validator::make(
            $data,
            [
                'price' => 'required',
                'cycle' => 'required',
                'mold' => 'required',
                'cycle' => 'required',
                'income' => 'required',
                'end' => 'required'
            ],
            [
                'required' => ":attribute不能为空",
            ],
            $attributes
        );
        if($validator->fails()){
            //判断加入的数据不能为空
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        }else{
            $model = Investment::findOrNew(0);
            $model->financialId = $id;
            $model->fill($data);
            $model->save();//写入数据库
            if($model && $model->id>0){
                $ret = ['state' => 'success', 'message' => '操作成功'];
            }else{
                $ret = ['state' => 'fail', 'message' => '操作失败'];
            }
        }
        return \Response::json($ret);
    }
    public function postDell($id)
    {
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if(Financial::destroy($id)){
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }
    public function anyOrder(Request $request)
    {
        //订单管理
        $items = DB::table('order');
        if($request->has('keyword') && $request->input('keyword')){
            $items->where('title','like','%'.$request->input('keyword').'%')
                ->orwhere('nickName','like','%'.$request->input('keyword').'%');

        }
        $items = $items
            ->leftJoin('financial','order.financialId','=','financial.id')
            ->leftJoin('users','users.id','=','order.userId')
            ->select('order.*','financial.title','users.nickName','users.idNo', 'users.name','users.mobile')
            ->orderBy('updated_at','desc')
            ->paginate(20);
        return view('admin.order.index',[
            'items' => $items
        ]);
    }
    public function postDel($id)
    {
        //订单管理的删除
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if(Order::destroy($id)){
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }
    public function getNews()
    {
        $news = DB::table('news')->get();
        return view('admin.news.index',[
            'news' => $news
        ]);
    }
    public function getNewsupdate($id=0)
    {
        //新闻发布
        $news = News::findOrNew($id); #findornew只能写在编辑和添加的页面，其他不能用
        $news['img'] = explode(',', $news['img']);
//        print_r($news['pic']);exit;
        return view('admin.news.update',[
            'news'=>$news
        ]);
    }
    public function postNewsupdate(Request $request,$id=0)
    {
        //新增也就是发布消息
        $model = News::findOrNew($id);
        $data = $request->all();

        unset($data['_token']);
        $attributes = [
            'title'=>'标题',
            'subtitle'=>'副标题',
            'content' => '内容',
        ];
        $validator = \Validator::make(
            $data,
            [
                'title' => 'required',
                'subtitle' => 'required',
                'content' => 'required',
            ],
            [
                'required' => ":attribute不能为空",
            ],
            $attributes
        );
        if($validator->fails()){
            //判断加入的数据不能为空
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        }else{
            $data['img'] = ( $request->has('img') ) ? implode(',', $data['img']) :  $data['img'] = '';
            $model->fill($data);
            $model->save();//写入数据库
            if($model && $model->id>0){
                $ret = ['state' => 'success', 'message' => '操作成功'];
            }else{
                $ret = ['state' => 'fail', 'message' => '操作失败'];
            }
        }
        return \Response::json($ret);
    }
    public function postDeletenews($id)
    {
        //订单管理的删除
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if(News::destroy($id)){
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }
    public function getQuestion()       #问题
    {
        $question= DB::table('question')->get();
       return view('admin.question.index',[
           'question'=>$question
       ]);
    }
    public function getUpdateqt($id=0)  #问题类型修改添加
    {
        $q = Question::findOrNew($id);
        return view('admin.question.update',[
            'q' => $q
        ]);
    }
    public function postUpdateqt(Request $request,$id=0)
    {
        $data = $request->all();
        $attributes = [
            'name'=>'类型名字',
            'title'=>'标题',
            'answer'=>'答案',
        ];
        $validator = \Validator::make(
            $data,
            [
                'name' => 'required',
                'title' => 'required',
                'answer' => 'required',
            ],
            [
                'required' => ":attribute不能为空",
            ],
            $attributes
        );
        if($validator->fails()){
            //判断加入的数据不能为空
            $message = $validator->messages();
            $ret['message'] = $message;
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['state'] = 'fail';
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        }else{
            $model = Question::findOrNew(0);
            $model->fill($data);
            $model->save();//写入数据库
            if($model && $model->id>0){
                $ret = ['state' => 'success', 'message' => '操作成功'];
            }else{
                $ret = ['state' => 'fail', 'message' => '操作失败'];
            }
        }
        return \Response::json($ret);
    }
    public function postDeleteqt($id)
    {
        //订单管理的删除
        $ret = ['state' => 'fail', 'message' => '未知错误，请刷新重试'];
        if(Question::destroy($id)){
            $ret = ['state' => 'success', 'message' => '删除成功'];
        }
        return \Response::json($ret);
    }
//    public function getQuestion()
//    {
//        $question = DB::table('question')->get();
//       return view('admin.question.index',[
//           'question' => $question
//       ]);
//    }

}